package com.feib.soeasy.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import com.feib.soeasy.dao.PayerDao;
import com.feib.soeasy.dao.PayerTempDao;
import com.feib.soeasy.dao.UploadFileDao;
import com.feib.soeasy.model.Payer;
import com.feib.soeasy.model.PayerTemp;
import com.feib.soeasy.model.UploadFile;
import com.feib.soeasy.pagination.PaginatedList;
import com.feib.soeasy.service.PayerTempService;
import com.feib.soeasy.util.UserUtil;

/**
 * @title (#)PayerTempServiceImpl.java<br>
 * @description <br>
 * @author Anson Tsai<br>
 * @version 1.0.0 2010/11/25
 * @copyright Far Eastern International Bank Copyright (c) 2010<br>
 * @2011/2/12 create by Anson Tsai<br>
 */
@Service("payerTempService")
public class PayerTempServiceImpl extends BaseServiceImpl implements PayerTempService {

    @Resource(name = "payerTempDao")
    private PayerTempDao payerTempDao;

    @Resource(name = "payerDao")
    private PayerDao payerDao;

    @Resource(name = "uploadFileDao")
    private UploadFileDao uploadFileDao;

    /*
     * (non-Javdoc)
     * 
     * @see
     * com.feib.soeasy.service.PayerTempService#getPayerTempByPk(java.lang.Long)
     */
    @Override
    public PayerTemp getPayerTempByPk(Long payerTempPk) {
        return payerTempDao.findByPk(payerTempPk);
    }

    /*
     * (non-Javdoc)
     * 
     * @see
     * com.feib.soeasy.service.PayerTempService#save(com.feib.soeasy.model.PayerTemp
     * )
     */
    @Override
    public PayerTemp save(PayerTemp p) {
        return payerTempDao.save(p);
    }

    /*
     * (non-Javdoc) 
     * 
     * @see
     * com.feib.soeasy.service.PayerTempService#delete(com.feib.soeasy.model
     * .PayerTemp)
     */
    @Override
    public void delete(PayerTemp p) {
        payerTempDao.delete(p);
    }

    @Override
    public PayerTemp getPayerTempNoConfirm(String groupNo, String payerNo) {
        List<PayerTemp> list = payerTempDao.getPayerTempNoConfirm(groupNo, payerNo);
        if (CollectionUtils.isNotEmpty(list)) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public boolean getPayerTempIsExist(String groupNo, String payerNo) {
        if (null != this.getPayerTempNoConfirm(groupNo, payerNo))
            return true;
        return false;
    }

    @Override
    public PaginatedList<PayerTemp> getPayerTempNoConfirm(PaginatedList<PayerTemp> paginationConfig) {
        return payerTempDao.getPayerTempNoConfirm(paginationConfig);
    }

    /* (non-Javadoc)
     * @see com.feib.soeasy.service.PayerTempService#updatePayerStatus(java.util.List, java.lang.String)
     */
    @Override
    public void updatePayerStatus(List<PayerTemp> checkPayerTemp, String payerStatus) {
        if (null == checkPayerTemp || 0 == checkPayerTemp.size())
            return;
        
        Date now = new Date();
        
        for(PayerTemp payerTemp : checkPayerTemp)
        {
            if (null == payerTemp || null == payerTemp.getPayerTempPk())
                continue;
            
            PayerTemp payerTempInDb = payerTempDao.findByPk(payerTemp.getPayerTempPk());
            if (null == payerTempInDb || null != payerTempInDb.getPayerStatus())
                continue;
            
            
            
            if (PayerTemp.PAYER_STATUS_CONFIRM_Y.equalsIgnoreCase(payerStatus))
            {
                // 放行
                
                if ("D".equalsIgnoreCase(payerTempInDb.getRecordStatus()))
                {
                    // 刪除
                    Payer payer = payerDao.getPayer(payerTempInDb.getGroup().getGroupNo(), payerTempInDb.getPayerNo());
                    if (null != payer)
                        payerDao.delete(payer);
                }
                else
                {
                    Payer payer = payerDao.getPayer(payerTempInDb.getGroup().getGroupNo(), payerTempInDb.getPayerNo());
                    if (null == payer)
                        payer = new Payer();
                    
                    payer.setGroup(payerTempInDb.getGroup());
                    payer.setPayerNo(payerTempInDb.getPayerNo());
                    payer.setPayerName(payerTempInDb.getPayerName());
                    payer.setPayerEmail(payerTempInDb.getPayerEmail());
                    payer.setPayerTelNo(payerTempInDb.getPayerTelNo());
                    payer.setPayerCellNo(payerTempInDb.getPayerCellNo());
                    payer.setLevelGround(payerTempInDb.getLevelGround());
                    payer.setParkingCount(payerTempInDb.getParkingCount());
                    payer.setHouseNo(payerTempInDb.getHouseNo());
                    payer.setPayerAddress(payerTempInDb.getPayerAddress());
                    payer.setPayerMemo(payerTempInDb.getPayerMemo());
                    
                    payerDao.save(payer);
                }
                
                payerTempInDb.setConfirmUser(UserUtil.getUser());
                payerTempInDb.setConfirmDate(now);
                payerTempInDb.setPayerStatus(PayerTemp.PAYER_STATUS_CONFIRM_Y);
                payerTempDao.save(payerTempInDb);
            }
            else
            {
                // 不放行
                //payerTempInDb.setPayerStatus(PayerTemp.PAYER_STATUS_CONFIRM_N);
                //payerTempDao.save(payerTempInDb);
                // 直接刪除
                payerTempDao.delete(payerTempInDb);
            }
        }
        
    }

    /* 
     * (non-Javadoc)
     * @see com.feib.soeasy.service.PayerTempService#updatePayerStatusByUploadFile(com.feib.soeasy.model.UploadFile, java.util.List)
     */
    @Override
    public void updatePayerStatusByUploadFile(List<UploadFile> checkUploadFile, Integer status) {
        
        if (null == checkUploadFile || 0 == checkUploadFile.size())
            return ;
        
        Date now = new Date();
        
        for(UploadFile uploadFile :checkUploadFile)
        {
            UploadFile tempUploadFile = uploadFileDao.getUploadFileByPk(uploadFile.getUploadFilePk());
            
            if (UploadFile.FILE_STATUS_CONFIRM_OK.equals(status))
            {
                // 放行
                tempUploadFile.setFileStatus(UploadFile.FILE_STATUS_CONFIRM_OK);
                tempUploadFile.setConfirmStatus(Boolean.TRUE);
                List<PayerTemp> payerTemps = payerTempDao.findByUploadFilePk(uploadFile.getUploadFilePk());
                
                for(PayerTemp payerTempInDb : payerTemps)
                {
                    Payer payer = payerDao.getPayer(payerTempInDb.getGroup().getGroupNo(), payerTempInDb.getPayerNo());
                    if (null == payer)
                        payer = new Payer();
                    
                    payer.setGroup(payerTempInDb.getGroup());
                    payer.setPayerNo(payerTempInDb.getPayerNo());
                    payer.setPayerName(payerTempInDb.getPayerName());
                    payer.setPayerEmail(payerTempInDb.getPayerEmail());
                    payer.setPayerTelNo(payerTempInDb.getPayerTelNo());
                    payer.setPayerCellNo(payerTempInDb.getPayerCellNo());
                    payer.setLevelGround(payerTempInDb.getLevelGround());
                    payer.setParkingCount(payerTempInDb.getParkingCount());
                    payer.setHouseNo(payerTempInDb.getHouseNo());
                    payer.setPayerAddress(payerTempInDb.getPayerAddress());
                    payer.setPayerMemo(payerTempInDb.getPayerMemo());
                    
                    payerDao.save(payer);
                    
                    payer = null;
                    
                    payerTempInDb.setConfirmUser(UserUtil.getUser());
                    payerTempInDb.setConfirmDate(now);
                    payerTempInDb.setPayerStatus(PayerTemp.PAYER_STATUS_CONFIRM_Y);
                    payerTempDao.save(payerTempInDb);
                }
            }
            else
            {                
                // 不放行
                tempUploadFile.setFileStatus(UploadFile.FILE_STATUS_CONFIRM_NOT_OK);
                tempUploadFile.setConfirmStatus(Boolean.FALSE);
                
                // 刪除戰存檔
                payerTempDao.deleteByUploadFilePk(uploadFile.getUploadFilePk());
            }
            
            // 更新上傳檔案狀態
            tempUploadFile.setConfirmDate(now );
            tempUploadFile.setConfirmUser(UserUtil.getUser());
            uploadFileDao.save(tempUploadFile);
        }
        
    }

    /* (non-Javadoc)
     * @see com.feib.soeasy.service.PayerTempService#queryByPayerTemp(com.feib.soeasy.model.PayerTemp, com.feib.soeasy.pagination.PaginatedList)
     */
    @Override
    public PaginatedList<PayerTemp> queryByPayerTemp(PayerTemp payerTemp, PaginatedList<PayerTemp> paginationConfig) {
        return payerTempDao.findByPayerTemp(payerTemp, paginationConfig);
    }

    /* (non-Javadoc)
     * @see com.feib.soeasy.service.PayerTempService#addPayerTemp(java.util.List)
     */
    @Override
    public void addPayerTemp(List<PayerTemp> payerTemps) {
        this.payerTempDao.addPayerTemp(payerTemps);
        
    }

    /* (non-Javadoc)
     * @see com.feib.soeasy.service.PayerTempService#deletePayerTempsByUploadFile(com.feib.soeasy.model.UploadFile)
     */
    @Override
    public void deletePayerTempsByUploadFile(UploadFile uploadFile) {
        this.payerTempDao.deletePayerTemp(uploadFile);
        
    }

    /* (non-Javadoc)
     * @see com.feib.soeasy.service.PayerTempService#getPayerTempIsExistByGroupPk(java.lang.Long, java.lang.String)
     */
    @Override
    public boolean getPayerTempIsExistByGroupPk(Long groupPk, String payerNo) {
        if (null != this.getPayerTempNoConfirmByGroupPk(groupPk, payerNo))
            return true;
        
        return false;
    }

    /* (non-Javadoc)
     * @see com.feib.soeasy.service.PayerTempService#getPayerTempNoConfirmByGroupPk(java.lang.Long, java.lang.String)
     */
    @Override
    public PayerTemp getPayerTempNoConfirmByGroupPk(Long groupPk, String payerNo) {
        List<PayerTemp> list = payerTempDao.getPayerTempNoConfirmByGroupPK(groupPk, payerNo);
        if (CollectionUtils.isNotEmpty(list)) {
            return list.get(0);
        }
        return null;
    }

    


}
